package com.edu.scau.dao.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;

public class SessionFactoryUtil {
	public static Session getSession(){
		
		SessionFactory sessionFactory = null;
		final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
		.configure().build();
		try {
			sessionFactory = new MetadataSources(registry).buildMetadata()
			.buildSessionFactory();
			} catch (Exception e) {
			// 出现异常需要销毁StandardServiceRegistry注册表
			StandardServiceRegistryBuilder.destroy(registry);
			}
		Session session = sessionFactory.openSession();
		return session;
		
	}

}
